//
//  SQLiteTable.m
//  etionMisc
//
//  Created by wjxfhxy on 5/10/13.
//  Copyright (c) 2013 GuangZhouXuanWu. All rights reserved.
//

#import "XMSSQLiteDBTable.h"

@implementation XMSSQLiteDBTable

static XMSQueueDictionary *s_dbcolpair = nil;

static XMSQueueDictionary *s_asdbtablepair = nil;

+ (void)registerDBTable:(NSString *)szDBTable asDBTable:(NSString *)szAsdbTable
{
    if (s_asdbtablepair == nil)
        s_asdbtablepair = [XMSQueueDictionary new];
    [s_asdbtablepair addKey:szAsdbTable value:szDBTable];
}

+ (NSArray *)getDBTablesAsDBTables:(NSArray *)arAstable
{
    NSMutableArray *ar = [[NSMutableArray new] autorelease_mm];
    for (NSString *table in arAstable)
    {
        BOOL bFind = NO;
        for (XMSKvPair *pair in s_asdbtablepair)
        {
            if ([pair.idKey isEqualToString:table] == YES)
            {
                [ar addObject:[NSString stringWithFormat:@"%@ as %@", pair.idValue, pair.idKey]];
                bFind = YES;
                break;
            }
        }
        if (bFind == NO)
            [ar addObject:table];
    }
    return ar;
}

+ (void)registerDBTable:(NSString *)szTable Cls:(Class)cls DBColPropertyPair:(XMSQueueDictionary *)qdPair
{
    if (s_dbcolpair == nil)
        s_dbcolpair = [XMSQueueDictionary new];
    [s_dbcolpair setValue:[XMSSQLiteDBTable registerTableClass:cls cloumnPropertyPair:qdPair] forKey:szTable];
}

+ (XMSQueueDictionary*)registerTableClass:(Class)cls cloumnPropertyPair:(XMSQueueDictionary *)qdPair
{
    for (NSUInteger i = 0; i < [qdPair count]; i++)
    {
        CDBColPropertyPair *pair = (CDBColPropertyPair *) [qdPair kvPairAtIndex:i];
        ENSObjectPropertyType type = [NSObject NSObjectPropertyType:[NSObject getPropertyType:cls property:pair.idValue]];
        pair.propertyType = type;
    }
    return qdPair;
}

+ (XMSQueueDictionary *)getDBTableClsValue:(NSString *)szTable cls:(id)cls DBCol:(NSArray *)arCol
{
    return [XMSSQLiteDBTable tableColumnMap:[s_dbcolpair getValueForKey:szTable] classObj:cls valueWithColoumn:arCol];
}

+ (XMSQueueDictionary *)tableColumnMap:(XMSQueueDictionary*)qdMap classObj:(NSObject*)clsObj valueWithColoumn:(NSArray *)arCol
{
    XMSQueueDictionary *kv = [[[XMSQueueDictionary alloc] initWithStoreClass:[CDBColPropertyPair class]] autorelease_mm];
    if (arCol.count > 0)
    {
        [arCol enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
        {
            for (NSUInteger i = 0; i < [qdMap count]; i++)
            {
                CDBColPropertyPair *pair = (CDBColPropertyPair *) [qdMap kvPairAtIndex:i];
                if ([(NSString *) obj isEqualToString:(NSString *) pair.idKey] == YES)
                {
                    id value = [clsObj valueForKey:(NSString *) pair.idValue];
                    CDBColPropertyPair *p = [[pair copy] autorelease_mm];
                    p.idValue = value;
                    [kv addKvPair:p];
                    break;
                }
            }
        }];
    }
    else
    {
        for (NSUInteger i = 0; i < [qdMap count]; i++)
        {
            CDBColPropertyPair *pair = (CDBColPropertyPair *) [qdMap kvPairAtIndex:i];
            id value = [clsObj valueForKey:(NSString *) pair.idValue];
            CDBColPropertyPair *p = [[pair copy] autorelease_mm];
            p.idValue = value;
            [kv addKvPair:p];
        }
    }
    return kv;
}

+ (XMSQueueDictionary *)getDBTableColPropertyPair:(NSString *)szTable DBCol:(NSArray *)arCol
{
    return [XMSSQLiteDBTable tableColumnMap:[s_dbcolpair getValueForKey:szTable] propertyWithColoumn:arCol];
}

+ (XMSQueueDictionary *)tableColumnMap:(XMSQueueDictionary*)qdMap propertyWithColoumn:(NSArray *)arCol
{
    XMSQueueDictionary *kv = [[[XMSQueueDictionary alloc] initWithStoreClass:[CDBColPropertyPair class]] autorelease_mm];
    if (arCol.count > 0)
    {
        [arCol enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
         {
             for (NSUInteger i = 0; i < [qdMap count]; i++)
             {
                 CDBColPropertyPair *pair = (CDBColPropertyPair *) [qdMap kvPairAtIndex:i];
                 if ([(NSString *) obj isEqualToString:(NSString *) pair.idKey] == YES)
                 {
                     [kv addKvPair:pair];
                     break;
                 }
             }
         }];
    }
    else
    {
        for (NSUInteger i = 0; i < [qdMap count]; i++)
        {
            CDBColPropertyPair *pair = (CDBColPropertyPair *) [qdMap kvPairAtIndex:i];
            [kv addKvPair:pair];
        }
    }
    return kv;
}

+ (void)initDBTableColPropertyPairWithFormatFilePath:(NSString*)formatFilePath
{
    
}
@end
